define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    
    
    require('../su/accordion/oaTabCtrl');
    
    $.widget('rk.colleagueContentCtrl', $.rk.oaContentCtrl, {
        _create: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var temp = require('oa/tmpl/colleague/content/tpl_colleague_content.tpl');
            var html = rk.templateText(temp, {
                uid: opt.uid,
                thisUid:SESSION.user.id
            });
            elem.html(html);
            elem.find("[act='colleague_content_user_follow']").beTip();
            elem.find("[act='colleague_user_contents_tabs']").tabs({
                active: 0,
                activate: function(e, ui) {
                    $.proxy(me.showUserContentTabs(e, ui), me);
                }
            });
            me._listeners();
        },
        followTab: 0,
        _listeners: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._colleague_content_listeners();
            elem.on("click", "[act='colleague_show_follow_tabs']", function() {
                me.followTab = 0;
                elem.find("[act='colleague_user_contents_tabs']").tabs({
                    active: 3
                });
            });
            elem.on("click", "[act='colleague_show_follow_fanse_tabs']", function() {
                me.followTab = 1;
                elem.find("[act='colleague_user_contents_tabs']").tabs({
                    active: 3
                });
            });
            elem.on('click', "[act='colleague_detail_more']", function() {
                elem.find("[act='colleague_user_contents_tabs']").tabs({
                    active: 1
                });
            });
            elem.on('click', "[act='colleague_content_user_follow']", function() {
                var button = $(this);
                var isFollow = button.attr("isFollow"); //elem.find("[act='colleague_content_user_follow']").attr("isFollow","0");
                var uid = button.attr("uid");
                if (isFollow == 1) {
                    rk.confirm(rk.i18n('COLLEAGUE_CONFIRM_UNFOLLOW'), function() {
                        rk.post("/json/oa_relation/delFollow.action", {
                            fId: uid
                        }, function(json) {
                            if (json.status == 0) {
                                button.attr("isFollow", "0");
                                button.attr("title", rk.i18n('COLLEAGUE_ADD_FOLLOW'));
                                elem.find("[act='colleague_content_user_follow']").removeClass("has_follow").addClass("no_follow");
                                rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                            } else {
                                rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                            }
                        });
                    });
                } else if (isFollow == 0) {
                    rk.post("/json/oa_relation/addFollow.action", {
                        fId: uid
                    }, function(json) {
                        if (json.status == 0) {
                            button.attr("isFollow", "1");
                            button.attr("title", rk.i18n('COLLEAGUE_CANCEL_FOLLOW'));
                            elem.find("[act='colleague_content_user_follow']").removeClass("no_follow").addClass("has_follow");
                            rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                        } else {
                            rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                        }
                    });
                }
            });
            elem.on("click", "[act='colleague_content_updateIcon']", function() {
                //colleague_content_updateIcon
                rk.dialog({
                    title: rk.i18n('COLLEAGUE_CHANGE_PROFILE'),
                    height: 600,
                    width: 800,
                    success: function(contentElem) {
                        require.async(["page/js/usersetting/controllers/usersettingIconCtrl"], function() {
                            contentElem.usersettingIconCtrl({
                                success: function(data) {
                                    if(data.picUrl) {
                                        var temp = '<img title="{{user.name}}" alt="{{user.name}}" src="{{picUrl.icon}}">';
                                        var html = rk.templateText(temp, data);
                                        elem.find("[act='colleague_content_updateIcon']").html(html);
                                        $('#pageleft').pageLeftCtrl('updateUserPic', data.picUrl.smallIcon);
                                    }
                                    rk.closeDialog();
                                }
                            });
                        });
                    }
                });
            });
            elem.on("click", "[act='integral_rule']", function() {
                //alert("积分规则")
            });
        },
        showUserContentTabs: function(e, ui) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (ui) {
                var divs = ui.newPanel;
                me.loaddata(divs.attr("loadDataType"), {}, divs);
            }
        },
        _colleague_content_listeners: function() {

        },
        _init: function() {
            var me = this;
            var elem = me.element;
            elem.hide();
            me.loaddata("index", {}, elem.find("#colleague_user_index"));
        },
        loaddata: function(type, param, divs) {
            var me = this;
            var elem = me.element;
            if (!param) {
                param = {
                    pageNo: 1,
                    pageSize: 20
                };
            }
            if (type && me.loadDataJson[type]) {
                $.proxy(me.loadDataJson[type].call(this, param, divs), me);
            }
        },
        loadDataJson: {
            "index": function(param, divs) {
                var me = this;
                var elem = me.element;
                var opt = me.options;
                divs.beLoading();
                rk.post("/json/oa_profile/index.action", {
                    u: opt.uid
                }, function(json) {
                    var temp = require("oa/tmpl/colleague/content/tpl_colleague_content_index.tpl");
                    if (json.status === 0) {
                        if (json.profile.userStatus && json.profile.userStatus == 1) {
                            elem.show();
                            var html = rk.templateText(temp, json);
                            divs.html(html);
                            elem.find("[act='colleague_user_name_title']").text(json.profile.userName);
                            elem.find("[act='colleague_user_depart_name_title']").text(json.profile.departName);
                            elem.find("[act='colleague_user_position_name_title']").text(json.profile.positionName);
                            if (json.profile.isSelf) {
                                elem.find("[act='colleague_detail_follow_div']").hide();
                            } else {
                                elem.find("[act='colleague_detail_follow_div']").show();
                            }
                            if (json.profile.isSelf || json.profile.userStatus != 1) {
                                elem.find("[act='colleague_content_user_follow']").hide();
                            } else {
                                elem.find("[act='colleague_content_user_follow']").show();
                                if (json.profile.myFollowStatus == 0) {
                                    elem.find("[act='colleague_content_user_follow']").removeClass("has_follow").addClass("no_follow");
                                    elem.find("[act='colleague_content_user_follow']").attr("title", rk.i18n('COLLEAGUE_ADD_FOLLOW'));
                                    elem.find("[act='colleague_content_user_follow']").attr("isFollow", "0");
                                } else {
                                    elem.find("[act='colleague_content_user_follow']").removeClass("no_follow").addClass("has_follow");
                                    elem.find("[act='colleague_content_user_follow']").attr("title", rk.i18n('COLLEAGUE_CANCEL_FOLLOW'));
                                    elem.find("[act='colleague_content_user_follow']").attr("isFollow", "1");
                                }
                            }
                            require.async(["core/apps/feed/feed"], function () {
                                var arCallbacks = [];
                                var height;
                                if(elem.parent().parent() && elem.parent().parent().parent().attr("act") == "oacontent"){
                                    height = elem.parent().parent().height();
                                }else{
                                    height = $("#rk-panel").height() - 50;
                                }
                                elem.height(height);
                                elem.beScroll({
                                    whileScrolling: function() {
                                        var scrollTop = Math.abs(parseInt(elem.find('.mCSB_container').css('top'), 10));
                                        if (scrollTop > elem.closest('.mCustomScrollbar').height()) {
                                            divs.find('a.js-gototop').show();
                                        } else {
                                            divs.find('a.js-gototop').hide();
                                        }
                                    },
                                    onTotalScroll: function() {
                                        for (var i = 0, len = arCallbacks.length; i < len; i++) {
                                            var fn = arCallbacks[i];
                                            if ($.isFunction(fn)) {
                                                fn();
                                            }
                                        }
                                    },
                                    onTotalScrollOffset: 120,
                                    alwaysTriggerOffsets: true
                                });
                                divs.find("[act='colleague_user_message']").feed2015({
                                    scrollRegister: function(fn) {
                                        arCallbacks.push(fn);
                                    },
                                    tabs: [{
                                        tabname: 'none',
                                        url: '/json/sns_feed/profiles.action?uid=' + opt.uid,
                                        appendmore: 'true'
                                    }],
                                    hasEditor: false
                                });
                            });
                        } else if(json.noPermission){
                            var temp = require("oa/tmpl/colleague/tpl_error_colleague.tpl");
                            var html = rk.templateText(temp, {
                                text: rk.i18n('COLLEAGUE_NO_AUTHORITY_ERROR')
                            });
                            elem.html(html);
                            elem.show();
                        } else if (!json.profile.userStatus) {
                            var temp = require("oa/tmpl/colleague/tpl_error_colleague.tpl");
                            var html = rk.templateText(temp, {
                                text: rk.i18n('COLLEAGUE_NO_ACCOUNT_ERROR')
                            });
                            elem.html(html);
                            elem.show();
                        } else if (json.profile.userStatus != 1) {
                            var temp = require("oa/tmpl/colleague/tpl_error_colleague.tpl");
                            var html = rk.templateText(temp, {
                                text: rk.i18n('COLLEAGUE_LEAVE_COMPANY_ERROR')
                            });
                            elem.html(html);
                            elem.show();
                        }
                    } else {
                        //rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                        var temp = require("oa/tmpl/colleague/tpl_error_colleague.tpl");
                        var html = rk.templateText(temp, {
                            text: rk.i18n('COLLEAGUE_CANT_VISIT_ERROR')
                        });
                        elem.html(html);
                        elem.show();
                    }
                });
            },
            "detail": function(param, divs) {
                var me = this;
                var elem = me.element;
                var opt = me.options;
                //param.gid = opt.gid;
                divs.beLoading();
                require.async(["./content/colleagueContentDetail"], function() {
                    divs.colleagueContentDetail({
                        uid: opt.uid
                    });
                });
            },
            "impression": function(param, divs) {
                var me = this;
                var elem = me.element;
                var opt = me.options;
                divs.beLoading();
                require.async(["./content/colleagueContentImpression.js"], function() {
                    divs.colleagueContentImpression({
                        uid: opt.uid
                    });
                });
            },
            "follow": function(param, divs) {
                var me = this;
                var elem = me.element;
                var opt = me.options;
                divs.beLoading();
                require.async(["./content/follow/colleagueContentFollow"], function() {
                    divs.colleagueContentFollow({
                        uid: opt.uid,
                        activeTabIndex: me.followTab
                    });
                });
            },
            "blog": function(param, divs) {
                var me = this;
                var elem = me.element;
                var opt = me.options;
                require.async(["../blog/blogTableCtrl.js"], function() {
                    divs.blogTableCtrl({
                        type: "profileBlog",
                        uid: opt.uid
                    });
                });
            },
            "file": function(param, divs) {
                var me = this;
                var elem = me.element;
                var opt = me.options;
                divs.beLoading();
                require.async(["./content/colleagueContentFile.js"], function() {
                    divs.colleagueContentFile({
                        type: "profileBlog",
                        uid: opt.uid
                    });
                });
            }
        }
    });
    return {
        init: function(rkContent, id) {
            rkContent.colleagueContentCtrl({
                uid: id
            });
        }
    }
});
